export enum CacheType {
  local,
  session
}

class Cache {
  setItem(key: string, value: any, type = CacheType.local) {
    type === CacheType.local
      ? localStorage.setItem(key, JSON.stringify(value))
      : sessionStorage.setItem(key, JSON.stringify(value))
  }

  getItem(key: string, type = CacheType.local) {
    const value =
      type === CacheType.local
        ? localStorage.getItem(key)
        : sessionStorage.getItem(key)
    return value ? JSON.parse(value) : value
  }

  removeItem(key: string, type = CacheType.local) {
    type === CacheType.local
      ? localStorage.removeItem(key)
      : sessionStorage.removeItem(key)
  }

  clear(type = CacheType.local) {
    type === CacheType.local ? localStorage.clear() : sessionStorage.clear()
  }
}

export default new Cache()
